package org.proddevplm.business;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.proddevplm.business.activity.ActivityAdapter;
import org.proddevplm.business.activity.ActivityController;
import org.proddevplm.business.administration.AdminAdapter;
import org.proddevplm.business.administration.AdminController;
import org.proddevplm.business.ajax.AjaxAdapter;
import org.proddevplm.business.ajax.AjaxController;
import org.proddevplm.business.api.AController;
import org.proddevplm.business.api.IAdapter;
import org.proddevplm.business.dataAccess.DataAccessAdapter;
import org.proddevplm.business.dataAccess.DataAccessController;
import org.proddevplm.business.diagram.DiagramAdapter;
import org.proddevplm.business.diagram.DiagramController;
import org.proddevplm.business.flow.FlowAdapter;
import org.proddevplm.business.flow.FlowController;
import org.proddevplm.business.openRoom.OpenRoomAdapter;
import org.proddevplm.business.openRoom.OpenRoomController;
import org.proddevplm.business.product.ProductAdapter;
import org.proddevplm.business.product.ProductController;
import org.proddevplm.business.project.ProjectAdapter;
import org.proddevplm.business.project.ProjectController;
import org.proddevplm.business.resource.ResourceAdapter;
import org.proddevplm.business.resource.ResourceController;
import org.proddevplm.business.tools.ToolsAdapter;
import org.proddevplm.business.tools.ToolsController;
import org.proddevplm.business.user.UserAdapter;
import org.proddevplm.business.user.UserController;
import org.proddevplm.constants.ControllerConstants;

public class ManagerFactory {
	
	private  Map<String, IAdapter> adapters = new HashMap<String, IAdapter>();
	private  Map<String, AController> controllers = new HashMap<String, AController>();
	
	
	public Object adapt(String manager, Integer command, Map<String, Object> data) {
		IAdapter adapter = adapters.get(manager);
		
		if (adapter == null) {
			if (ControllerConstants.USER_MANAGER.equals(manager)) {
				adapter = new UserAdapter();
			}
			else if (ControllerConstants.FLOW_MANAGER.equals(manager)){
				adapter = new FlowAdapter();
			}
			else if (ControllerConstants.PROJECT_MANAGER.equals(manager)){
				adapter = new ProjectAdapter();
			}
			else if (ControllerConstants.PRODUCT_MANAGER.equals(manager)){
				adapter = new ProductAdapter();
			}
			else if (ControllerConstants.TOOLS_MANAGER.equals(manager)){
				adapter = new ToolsAdapter();
			}
			else if (ControllerConstants.DATA_ACCESS_MANAGER.equals(manager)){
				adapter = new DataAccessAdapter();
			}
			else if (ControllerConstants.AJAX_MANAGER.equals(manager)){
				adapter = new AjaxAdapter();
			}
			else if(ControllerConstants.RESOURCE_MANAGER.equals(manager)){
				adapter = new ResourceAdapter();
			}
			else if(ControllerConstants.ADMIN_MANAGER.equals(manager)){
				adapter = new AdminAdapter();
			}
			else if(ControllerConstants.ACTIVITY_MANAGER.equals(manager)){
				adapter = new ActivityAdapter();
			}
			else if(ControllerConstants.DIA_MANAGER.equals(manager)){
				adapter = new DiagramAdapter();
			}
			else if(ControllerConstants.OPEN_ROOM_MANAGER.equals(manager)){
				adapter = new OpenRoomAdapter();
			}
			
			adapters.put(manager, adapter);
		}
		
		return adapter.adapt(command, data);
	}
	
	public Object execute(String manager,Integer command, Object data, Map<String, Object> stateMap, HttpSession session) {
		AController controller = controllers.get(manager);
		
		if (controller == null) {
			if (ControllerConstants.USER_MANAGER.equals(manager)) {
				controller = new UserController();
			}
			else if (ControllerConstants.FLOW_MANAGER.equals(manager)){
				controller = new FlowController();
			}
			else if (ControllerConstants.PROJECT_MANAGER.equals(manager)){
				controller = new ProjectController();
			}
			else if (ControllerConstants.PRODUCT_MANAGER.equals(manager)){
				controller = new ProductController();
			}
			else if (ControllerConstants.TOOLS_MANAGER.equals(manager)){
				controller = new ToolsController();
			}
			else if (ControllerConstants.DATA_ACCESS_MANAGER.equals(manager)){
				controller = new DataAccessController();
			}
			else if (ControllerConstants.AJAX_MANAGER.equals(manager)){
				controller = new AjaxController();
			}
			else if (ControllerConstants.RESOURCE_MANAGER.equals(manager)){
				controller = new ResourceController();
			}
			else if (ControllerConstants.ADMIN_MANAGER.equals(manager)){
				controller = new AdminController();
			}
			else if (ControllerConstants.ACTIVITY_MANAGER.equals(manager)){
				controller = new ActivityController();
			}
			else if (ControllerConstants.DIA_MANAGER.equals(manager)){
				controller = new DiagramController();
			}
			else if (ControllerConstants.OPEN_ROOM_MANAGER.equals(manager)){
				controller = new OpenRoomController();
			}

			controllers.put(manager, controller);
		}
		
		controller.setStateMap(stateMap);
		controller.setSession(session);
		
		return controller.execute(command, data);
	}

}
